package com.csun.cmny.provider.consumer;

import cn.jpush.api.push.model.PushPayload;
import com.csun.cmny.base.constant.MessageConstant;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.consumer.message.jpush.JPushMessageBuilder;
import com.csun.cmny.provider.exceptions.OpcBizException;
import com.csun.cmny.provider.model.dto.SendMessageRequest;
import com.csun.cmny.provider.consumer.message.MessageSender;
import com.csun.cmny.provider.model.vo.message.FlowMessageVo;
import com.csun.cmny.provider.model.vo.message.MessageWayVo;
import com.csun.cmny.provider.service.OpcFlowMessageGroupWayService;
import com.csun.cmny.util.JacksonUtil;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-07 14:26
 * @Description:
 */
@Slf4j
@Service
public class OpcSendMessageTopicConsumer {

    @Resource
    private OpcFlowMessageGroupWayService opcFlowMessageGroupWayService;
    @Resource
    private JPushMessageBuilder jPushMessageBuilder;

    private final Map<String, MessageSender> messageSenderMap;

    @Autowired
    public OpcSendMessageTopicConsumer(Map<String, MessageSender> messageSenderMap) {
        this.messageSenderMap = messageSenderMap;
    }

    public void handlerSendMessageTopic(String body, String topicName, String tags, String keys) {

        SendMessageRequest sendMessageRequest;

        try {
            sendMessageRequest = JacksonUtil.parseJson(body, SendMessageRequest.class);

        } catch (Exception e) {
            log.error("sendMessageRequest <== JSON 转换异常={}", e.getMessage(), e);
            throw new IllegalArgumentException("sendMessageRequest <== JSON 转换异常", e);
        }

        String msgType = sendMessageRequest.getMsgType();
        if (StringUtils.isEmpty(msgType)) {
            throw new IllegalArgumentException("msgType不能为空");
        }

        String msgCode = sendMessageRequest.getMsgCode();
        Long groupId = sendMessageRequest.getGroupId();

        checkSendMessageRequest(sendMessageRequest);

        try {

            // 1、查询消息
            FlowMessageVo flowMessageVo = opcFlowMessageGroupWayService.queryGroupMessageWayWithCodeAndGroupId(msgCode, groupId);
            if (PublicUtil.isEmpty(flowMessageVo)) {
                throw new OpcBizException(ErrorCodeEnum.OPC10050001);
            }

            String roleCodeList = flowMessageVo.getRole();
            if (PublicUtil.isNotEmpty(roleCodeList)) {
                sendMessageRequest.setRoleCodeList(Arrays.asList(roleCodeList.split(",")));
            }
            sendMessageRequest.setMsgCode(flowMessageVo.getCode());
            sendMessageRequest.setMessageUrl(flowMessageVo.getUrl());
            sendMessageRequest.setShow(flowMessageVo.getShow());
            sendMessageRequest.setMessageName(flowMessageVo.getName());
            if (PublicUtil.isNotEmpty(flowMessageVo.getMessage())) {
                if (!flowMessageVo.getMessage().equals("system_message"))
                sendMessageRequest.setMessage(flowMessageVo.getMessage());
            }
            sendMessageRequest.setMessageTitle(flowMessageVo.getTitle());

            checkFlowMessage(flowMessageVo);

            List<MessageWayVo> messageWayList = flowMessageVo.getMessageWayVoList();
            if (PublicUtil.isEmpty(messageWayList)) {
                throw new OpcBizException(ErrorCodeEnum.OPC10050002);
            }

            PushPayload pushPayload = null;
            // 2、发送消息
            for (MessageWayVo way : messageWayList) {
                String type = way.getType();
                sendMessageRequest.setWay(type);
                if (MessageConstant.MESSAGE_WAY_WEBSOCKET.equals(type)) {
                    messageSenderMap.get(way.getCode()).sendMessage(sendMessageRequest);
                }
                if (MessageConstant.MESSAGE_WAY_JPUSH.equals(type)) {
                    if (PublicUtil.isEmpty(pushPayload)) {
                        pushPayload = jPushMessageBuilder.build(sendMessageRequest);
                    }
                    log.info("sendMessageRequest pushPayload ==> {}", pushPayload.toString());
                    sendMessageRequest.setPushPayload(pushPayload);
                    messageSenderMap.get(way.getCode()).sendMessage(sendMessageRequest);
                }
            }

        } catch (Exception e) {
            log.error("消息发送异常={}", e.getMessage(), e);
            throw new IllegalArgumentException("消息发送异常", e);
        }
    }

    private void checkFlowMessage(FlowMessageVo flowMessageVo) {
    }

    private void checkSendMessageRequest(SendMessageRequest sendMessageRequest) {
    }
}
